package com.example.repair

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.utils.Consts
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.RepairBeanItem
import com.example.base.http.Cons
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.repair.adapter.RepairImgAdapter
import com.example.repair.databinding.ActivityRepairAddBinding
import com.example.repair.databinding.ActivityRepairBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/repair/RepairAddActivity")
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding, RepairIntent, RepairViewModel>() {
    override val binding by lazy {
        ActivityRepairAddBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<RepairViewModel>()
    val repairImgAdapter by lazy { RepairImgAdapter() }
    var areaName="A区停车场"
    override fun initView() {
        binding.headRepairAdd.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("维修添加")
            headAdd.setImageResource(R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val currentTime = System.currentTimeMillis()
                val phone = binding.repairaddPhone.text.toString()
                val remark = binding.repairaddRemark.text.toString()
                val user = binding.repairaddUser.text.toString()
                val item = RepairBeanItem(
                    phone,
                    remark,
                    "",
                    null,
                    "WX${vm.getDataTimeString(currentTime)}",
                    "待处理",
                    areaName,
                    user,
                    vm.getDataTime(currentTime),
                    SPUtils.getInstance().getInt(Cons.UID)
                )
                vm.sendIntent(RepairIntent.addRepair(item))
            }
        }

        binding.repairaddRv.apply {
            adapter=repairImgAdapter
            layoutManager= GridLayoutManager(this@RepairAddActivity,3)
        }

        binding.repairaddImg.setOnClickListener {
            if (repairImgAdapter.data.size == 4){
                ToastUtils.showLong("只能传4张")
                return@setOnClickListener
            }
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.repairaddUser.setText(SPUtils.getInstance().getString(Cons.UNAME))

        val areaList = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter  = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        areaAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairaddLocarion.adapter = areaAdapter
        binding.repairaddLocarion.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                areaName = areaList[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(bean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        ToastUtils.showLong("报修完成")
                        finish()
                    }

                    is UiState.onError -> {
                        Log.i("lr", it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

}